﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

namespace badminton.Pages
{
    public partial class TrainingDetail : System.Web.UI.Page
    {
        protected global::AjaxControlToolkit.Accordion MyAccordion;

        protected void Page_Load(object sender, EventArgs e)
        {

            Training training =  getCurrTraining();

            if (training == null)
            {
                return;
            }


            setTheAccordion(LabelTrainingLevel.Text, training);


            if (!IsPostBack)
            {
                Label5.Text = "";
                if (training.Hall!=null)
                {
                    Label5.Text = "&gt;&gt;<a href='halldetail.aspx?hallid=" + training.hallId+"'>"+training.Hall.name+"</a>";
                    if (training.Hall.CityArea!=null)
                    {
                        Label5.Text = "培训介绍&gt;&gt;"+training.Hall.CityArea.name + Label5.Text;
                    }
                }

                Label1.Text = training.name;

                EditorPXTS.Content = training.description;
                EditorZYSX.Text = training.attentions;
                EditorFYXQ.Text = training.costDetail;

                Repeter1.DataSource = training.TrainingCourse;
                Repeter1.DataBind();
            }


        }

        protected Training getCurrTraining()
        {
            int traingId = Convert.ToInt32(this.Request.QueryString["trainingid"]);
            BadmintonEntities bde = new BadmintonEntities();
            Training training = bde.Training.FirstOrDefault(p => p.id == traingId);
            if (training == null)
            {
                training = bde.Training.FirstOrDefault();
            }
            return training;
        }

        protected void setTheAccordion(string by, Training training)
        {
            BadmintonEntities bde = new BadmintonEntities();
 
            int selIdx = 0;
            MyAccordion.Panes.Clear();
            if (by == "hall")
            {
                foreach (Hall hall in bde.Hall)
                {
                    AjaxControlToolkit.AccordionPane panel = new AjaxControlToolkit.AccordionPane();
                    panel.ID = "AccordPaneHall"+ hall.id.ToString();
                    panel.HeaderContainer.Controls.Add(new LiteralControl(hall.name));

                    foreach (Training ta in hall.Training.OrderBy(p => p.id))
                    {
                        HyperLink hlChildMenu = new HyperLink();
                        hlChildMenu.Text = ta.name + "<br/>";
                        hlChildMenu.NavigateUrl = "~\\Pages\\TrainingDetail.aspx?trainingid=" + ta.id.ToString();
                        panel.ContentContainer.Controls.Add(hlChildMenu);
                    }
                    if (training != null && training.Hall != null)
                    {
                        if (training.hallId == hall.id)
                        {
                            selIdx = MyAccordion.Panes.Count;
                        }
                    }
                    MyAccordion.Panes.Add(panel);
                }    
            }
            else
            {
                foreach (CityArea ca in bde.CityArea)
                {
                    AjaxControlToolkit.AccordionPane panel = new AjaxControlToolkit.AccordionPane();
                    panel.ID = "AccordPane" + ca.id.ToString();
                    panel.HeaderContainer.Controls.Add(new LiteralControl(ca.name));

                    foreach (Hall hl in ca.Hall.OrderBy(p => p.id))
                    {
                        foreach (Training ta in hl.Training.OrderBy(p => p.id))
                        {
                            HyperLink hlChildMenu = new HyperLink();
                            hlChildMenu.Text = ta.name + "<br/>";
                            hlChildMenu.NavigateUrl = "~\\Pages\\TrainingDetail.aspx?trainingid=" + ta.id.ToString();
                            panel.ContentContainer.Controls.Add(hlChildMenu);
                        }
                    }
                    if (training != null && training.Hall!=null)
                    {
                        if (training.Hall.cityAreaId == ca.id)
                        {
                            selIdx = MyAccordion.Panes.Count;
                        }
                    }
                    MyAccordion.Panes.Add(panel);
                }
            }


            MyAccordion.SelectedIndex = selIdx;
        }

        public string GetImageURL(object id)
        {
            int courseid = (int) id;
            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourse course = bde.TrainingCourse.FirstOrDefault(p => p.id == courseid);

            if (course==null)
            {
                return "";
            }

            return "";
        }

        public string GetTime(object id)
        {
            int courseid = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourse course = bde.TrainingCourse.FirstOrDefault(p => p.id == courseid);

            if (course == null)
            {
                return "";
            }

            string ret="";
            if (course.startTime!=null)
                ret = course.startTime.Value.ToString("MM.dd hh:mm");

            ret = ret + " - ";

            if (course.endTime != null)
                ret = ret + course.endTime.Value.ToString("MM.dd hh:mm");


            return ret;


        }

        protected void OnChangeTrainLevel(object sender, CommandEventArgs e)
        {
            LabelTrainingLevel.Text = e.CommandName.ToString();
            setTheAccordion(LabelTrainingLevel.Text,getCurrTraining());
        }
    }
}